
function highlightCurrentPageName() {
	var namePossition = document.URL.lastIndexOf("/", document.URL.length) + 1;
	var pageName = document.URL.substring(namePossition, document.URL.length - ".xhtml".length);
	document.getElementById(pageName).className = "highlightedMenuItem";
}

//======================================================================================
		
function addPlacehodersOnLoginForm() {
	var email = document.getElementById("loginForm:email");
	var password = document.getElementById("loginForm:password");
	email.setAttribute("placeholder","Email");
	password.setAttribute("placeholder","Password");
}

function hideElement(id) {
	document.getElementById(id).setAttribute("style", "display: none;");
}

function unhideElement(id) {
	document.getElementById(id).setAttribute("style", "");
}

function cleanElementValue(id) {
	document.getElementById(id).nodeValue ="";
}

function cleanInputs(ids) {
	for (var i = 0; i < ids.length; i++) {
		var id = ids[i];
		document.getElementById(id).value ="";
	}
}

function handleOfferSubmission() {
	hideElement("localizedErrorMessage");
	
	var value=document.getElementById("newOffer:title").value;
	if (value==null || value=="")
	{
		unhideElement("localizedErrorMessage");
		return false;
	}
	hideImagePreview();
	hideRemoveImageLink();
	unhideElement("imageUploadButton");
	hideElement("fromContainer");
	unhideElement("addNewOfferButton");
}

function handleOfferClearing() {
	hideElement("localizedErrorMessage");
	cleanInputs(["newOffer:title","newOffer:price","newOffer:description","newOffer:imageId"]); 
	hideImagePreview();
	hideRemoveImageLink();
	unhideElement("imageUploadButton");
	hideElement("fromContainer"); 
	unhideElement("addNewOfferButton");
}

function hideImagePreview() {
	document.getElementById("imagePreviewContainer").removeChild(document.getElementById("imagePreviewContainer").lastChild);
}


//======================================================================================
// dynamic offers rendering
function handleOfferNotification(notificationString){
	var message = JSON.parse(notificationString);
	
	if(message.type == "OfferClosedNotification") {
		updateSoldOfferView(message.offerId);
	}
	if(message.type == "OfferRemovedNotification") {
		removeOffer(message.offerId);
	}
	if(message.type == "NewOfferNotification") {
		addNewOffer();
	}
	if (message.type == "ImageStoredNotification") {
		handleImageploadedEvent(message);
	}
}

function handleImageploadedEvent(notification) {
	var element = document.getElementById("newOffer:imageId");
	element.value = notification.imageId;
	element.onchange();
	hideUploadingGif();
	showRemoveImageLink();
	previewImage(notification.imageId);
}

function previewImage(imageid) {
	var url = dynamicImageUrlTemplate + imageid;
	var img = document.createElement("img");
	img.setAttribute("src", url);
	document.getElementById("imagePreviewContainer").appendChild(img);
}

function updateSoldOfferView(offerId) {
	var actionsContainer = document.getElementById("actionsContainer-" + offerId);
	actionsContainer.innerHTML = "";
	var span = document.createElement("span");
	span.className = "btn-block btn-large soldText";
	
	var b = document.createElement("b");
	b.innerHTML = localizedSoldMessage;
	
	span.appendChild(b);
	
	actionsContainer.appendChild(span);
	
	var offerImage = document.getElementById("offerImage-" + offerId);
	if (offerImage) {
//		<img id="soldImage-#{offer.id}" src="#{request.contextPath}/img/sold_ribbon.png" alt="" class="soldRibbon" />
		var soldImageStamp = document.createElement("img");
		soldImageStamp.id = "soldImage-" + offerId;
		soldImageStamp.src = contextPath + "/img/sold_ribbon.png";
		soldImageStamp.className = "soldRibbon";
		
		offerImage.parentNode.insertBefore(soldImageStamp, offerImage);
	}
}

function removeOffer(offerId) {
	var elementId = "offer-".concat(offerId);
	var element = document.getElementById(elementId);
	if (element) {
		element.parentNode.removeChild(element);
	} else {
		console.log("can't remove element " + elementId);
	}
}

function addNewOffer() {
	var refreshButton = document.getElementById("offersForm:formRefresher");
	refreshButton.click();
}

function clickOn(elementId) {
	var element = document.getElementById(elementId);
	if (element) {
		element.click();
	}
	return false;
}

function uploadImage(elementId) {
	
	var file = document.getElementById(elementId).files[0]; 

    if (file) {
    	hideElement("imageUploadButton");
    	showUploadingGif();
    	offersClient.send(file);
    }
}

function removeImage() {
	hideRemoveImageLink();
	cleanInputs(["newOffer:imageId", "hiddenImageUpload"]);
	hideImagePreview();
	unhideElement("imageUploadButton");
	return false;
}

function showUploadingGif() {
	document.getElementById("loadingImage").className  = "";
}

function hideUploadingGif() {
	document.getElementById("loadingImage").className  = "hidden";
}

function showRemoveImageLink() {
	document.getElementById("newOffer:removeImage").className  = "";
}

function hideRemoveImageLink() {
	document.getElementById("newOffer:removeImage").className  = "hidden";
}